package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.dllo.findfood.R;

import java.io.FileNotFoundException;

/**
 * Created by dllo on 15/11/7.
 */
public class ChoseImageActivity extends Activity implements View.OnClickListener {
    private ImageView share_image;
    private TextView next,relection;
    private  ContentResolver cr ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chose_image);
        initView();

    }
    private void  initView(){

       share_image = (ImageView) findViewById(R.id.share_image);
        share_image.setOnClickListener(this);


       next = (TextView) findViewById(R.id.nextChose);
       next.setOnClickListener(this);
       relection = (TextView) findViewById(R.id.re_election);
        cr = getContentResolver();
       relection.setOnClickListener(this);
        }
private  Bitmap bitmap;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.nextChose:
                Intent intent_send = new Intent(this,SendActivity.class);
                 intent_send.putExtra("Image",bitmap);
                 startActivityForResult(intent_send, 400);


            break;
            case R.id.re_election:
                getImage();
                break;
            case R.id.share_image:
                getImage();
                break;
            default:
                break;
        }
    }

    private void getImage() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//多媒体--图片--地址 跳转到图库地址
        startActivityForResult(intent, 100);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //传递图片
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();

            ContentResolver contentResolver = this.getContentResolver();
            try {
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inSampleSize = 4;
                bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri),null,opts);
                ImageView imageView = (ImageView) findViewById(R.id.share_image);
                /* 将Bitmap设定到ImageView */
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {

            }
        }
        //访问本地图片的方法
        if (data != null && requestCode == 100 && resultCode == RESULT_OK) {
            Uri uri = data.getData();//图片中数据库的地址--图库地址
            Cursor cursor = cr.query(uri, null, null, null, null);
            if (cursor!= null) {

                //遍历cursor对象
                while (cursor.moveToNext()) {
                    //获取文件路径地址
                    String imagePath= cursor .getString
                            (cursor.getColumnIndex(
                                    MediaStore.Images.Media.DATA));//取data字段 也就是图片对应的地址
                    //通过图片工厂去解析并显示
                    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    share_image.setImageBitmap(bitmap);

                }


                cursor.close();
            }
        }
    }
}
